const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// 富文本工具类
const formatRichText = (html) => {
  let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');


  //判断img是否存在
  if (newContent.indexOf('img') != -1) {
    var result = newContent.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
      // console.log(capture);
      return '<img src=' + capture + ' style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"/>';
    });
    return result;
  }


  return newContent;
}
// 清新颜色随机取
const getColor = (html) => {
  // var A_BG = ['#99CCFF', '#ffbc80', '#8bc34a', '#ffc107', '#00bcd4', '#03a9f4', '#f44336', '#e970f5', '#a7e22a', '#2bd1ff']
  var A_BG = ['#CCFF99', '#99CCFF', '#CCFFFF', '#FFCCCC', '#CCCC99', '#CCFFCC', '#CCCCFF', '#66CCCC', '#99CCFF', '#99CCFF', '#CCCCCC', '#CCFF99', '#FF99CC', '#CCCCFF', '#99CCCC', '#99CCFF', '#CCFFFF', '#FF9999', '#CCCCFF', '#ABCDEF'];
  var num = Math.floor(Math.random() * 20)
  var color = A_BG[num]
  return color
}
// 检查是否登录
const checkLogin = () => {
  return new Promise(function (resolve, reject) {
    if (wx.getStorageSync('userId') && wx.getStorageSync('token')) {
      resolve(true)
    } else {
      reject(false);
    }
  });
}


// 拿提示的第一个
const forin = (data) => {
  console.log(data)
  var arr = []
  for (let i in data) {
    arr.push(data[i])
  }
  return arr[0]
}
// 数值转换   
const numTransform = (data) => {
  var num = +data
  if (num < 1000) {
    return num
  }

  if (num > 1000) {

    var value = num / 10000
    value = value.toFixed(1);
    return value + "万"
  }

}
export const checkPermission = () => {
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.record']) {
        wx.authorize({
          scope: 'scope.record',
          success() {
            // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
          },
          fail: () => {
            wx.showModal({
              title: '缺少录音权限',
              content: '请前往设置打开小程序设置',
              confirmText: '前往设置',
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.openSetting({
                    success(res) {
                      console.log(res.authSetting)
                      checkPermission()
                    }
                  })
                }
              },
            })
          }
        })
      }


      wx.getSystemInfo({
        success(res) {
          const microphoneAuthorized = res.microphoneAuthorized
          console.log('麦克风权限:' + microphoneAuthorized)
          // cameraAuthorized & microphoneAuthorized 在 小程序 lib 2.6.0 开始支持
          if (microphoneAuthorized === false) {
            wx.showModal({
              title: '缺少权限',
              content: '请前往系统设置打开微信的麦克风权限',
              confirmText: '知道了',
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  checkPermission()
                }
              },
            })
          }
        }
      })
    }
  })
}

const config = function () {
  return {
    api: `https://52mozi.com/api`, // 在线api
    gameApi: `https://52mozi.com`, //在线game

    // api: `http://146.56.216.9:5000/api`, // 在线api
    // gameApi: `http://146.56.216.9:5000`, //在线game

    // api: `https://hxyw.net/api`, // 在线api
    // gameApi: `https://hxyw.net`, //在线game

    // api: `http://192.168.0.120:5000/api`, //本地api
    // gameApi: `http://192.168.0.120:5000`, //本地game

    token: wx.getStorageSync('token'),
    userId: wx.getStorageSync('userId'),
    
    appUrl: "http://web.52yuze.com/#/pages/download/download"


  }
}
module.exports = {
  formatTime: formatTime,
  config: config,
  formatRichText: formatRichText,
  getColor: getColor,
  checkLogin: checkLogin,
  forin: forin,
  numTransform: numTransform,
  checkPermission: checkPermission
}